package data;

import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;

public class EVESystem {

  private String                                  _systemPrimaryKey;
  private String                                  _systemID;
  private String                                  _systemName;
  private HashMap<EVEItem, EVECentralMarketOrder> _systemMarketDataMap;
  
  public EVESystem(String pk, String id, String name) throws IOException {
    _systemPrimaryKey = pk;
    _systemID = id;
    _systemName = name;
    _systemMarketDataMap = new HashMap<EVEItem, EVECentralMarketOrder>();
  }

  public void _addItem(EVEItem item) throws IOException {
    EVECentralMarketOrder order = new EVECentralMarketOrder();
    _systemMarketDataMap.put(item, order);
    item.getItemMarketDataMap().put(this, order);
  }

  public void _removeItem(EVEItem eveItem) {
    _systemMarketDataMap.remove(eveItem);
  }
  
  public void dump(){
    System.out.println("PK = " + _systemPrimaryKey);
    System.out.println("ID = " + _systemID);
    System.out.println("Name = " + _systemName);
  }

  public String _dumpToString() {
    String dumpString = _systemName;
    for (Iterator<EVEItem> IT = _systemMarketDataMap.keySet().iterator(); IT
        .hasNext();) {
      EVEItem key = (EVEItem) IT.next();
      EVECentralMarketOrder order = _systemMarketDataMap.get(key);
      dumpString += "\n" + key + "\n" + order._dumpToString();
    }
    return dumpString;
  }

  @Override
  public String toString() {
    return _systemName;
  }
  
  @Override
  public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + ((_systemPrimaryKey == null) ? 0 : _systemPrimaryKey.hashCode());
    return result;
  }

  @Override
  public boolean equals(Object obj) {
    if (this == obj) return true;
    if (obj == null) return false;
    if (getClass() != obj.getClass()) return false;
    EVESystem other = (EVESystem) obj;
    if (_systemPrimaryKey == null) {
      if (other._systemPrimaryKey != null) return false;
    }
    else if (!_systemPrimaryKey.equals(other._systemPrimaryKey)) return false;
    return true;
  }

  public String getSystemPrimaryKey(){
    return _systemPrimaryKey;
  }
  
  public String getSystemID(){
    return _systemID;
  }
  
  public String getSystemName(){
    return _systemName;
  }
  
  public HashMap<EVEItem, EVECentralMarketOrder> getSystemMarketDataMap(){
    return _systemMarketDataMap;
  }
}
